package cn.tedu.rebbimq.simple;

import com.rabbitmq.client.*;

import java.io.IOException;

import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 1.连接
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setUsername("admin");
        f.setPassword("admin");
        f.setVirtualHost("/ryan");
        Connection con = f.newConnection();
        Channel c = con.createChannel();
        // 2.定义队列 helloWord
//        c.queueDeclare(
//                "helloword",
//                false,
//                false,
//                false,
//                null
//        );

        DeliverCallback deliverCallback = new DeliverCallback() {
            @Override
            public void handle(String s, Delivery delivery) throws IOException {
                byte[] a = delivery.getBody();
                String msg = new String(a);
                System.out.println("收到: " + msg);


            c.basicQos(1);
            c.basicAck(delivery.getEnvelope().getDeliveryTag(),false);
                System.out.println("处理完成!");
            }


        };

        CancelCallback cancelCallback = new CancelCallback() {
            @Override
            public void handle(String s) throws IOException {

            }
        };


        // 3.pop 弹出消息
        c.basicConsume(
                "helloworld",
                false, //关闭自动确认,改成手动确认
                deliverCallback,
                cancelCallback
                );
    }
}
